<?php
declare (strict_types = 1);

namespace app\home\controller;

use think\facade\View;
use think\Request;


class Cart extends Base
{
    /**
     * 展示购物车列表
     * @return string
     */
    public function cart(){
        # 查询购物车数据
        $data =  \app\home\business\Cart::selectCart();
        View::assign('data',$data);
        return View::fetch();
    }

    /**
     * 添加商品至购物车
     */
    public function addCart(){
        $sku_id = \request()->post('sku_id');
        $number = \request()->post('number');
        # 添加购物车
        \app\home\business\Cart::addCart($sku_id,$number);
        # 查询添加商品详情
        $sku_info =  \app\common\model\SpecGoods::find($sku_id)->toArray();
        # 查询商品基本信息
        $goods_info =  \app\common\model\Goods::find($sku_info['goods_id'])->toArray();
        $data = [
            'goods_id' => $goods_info['id'],
            'spec_goods_id' => $sku_info['id'],
            'goods_logo' => $goods_info['goods_logo'],
            'goods_name' => $goods_info['goods_name'],
            'value_names' => $sku_info['value_names'],
            'number' => $number,
        ];
        View::assign('data',$data);
        return View::fetch('success-cart');
    }

    /**
     * 修改数量
     * @return int
     */
    public function changeNumber(){
        $id = \request()->post('id');
        $current_number = \request()->post('current_number');
        if (!is_numeric($id) || !is_numeric($current_number)) {
            return fail();
        }
        \app\home\business\Cart::changNumber($id,$current_number);
        return  success();
    }

    /**
     * 修改购物车商品选中状态
     * @return \think\response\Json|void
     */
    public function changeSelected(){
        $id = \request()->post('id');
        $is_selected = \request()->post('is_selected');
        if (!is_numeric($id) || !is_numeric($is_selected)) {
            return fail();
        }
        \app\home\business\Cart::changeSelected($id,$is_selected);
        return  success();
    }

    /**
     * 购物车删除
     * @return \think\response\Json|void
     */
    public function cartDelete(){
        $id = \request()->post('id');
        if (!is_numeric($id)) {
            return fail();
        }
        \app\home\business\Cart::cartDelete($id);
        return  success();
    }
}
